﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq.SqlClient;
using System.Data.Linq;

namespace Hired
{
    /*  An AdminStaffAccount for administration use.    */
    public static class DBAdminStaffAccount
    {
        //Add an adminstaffaccount
        public static bool addAdminStaffAccount(AdminStaffAccount a) {
            try
            {
                a.ASAccountID = "tempvalue";
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.AddToAdminStaffAccounts(a);
                    db.SaveChanges();
                }
                return true;
            }
            catch {
                return false;
            }
            
        }

        //Retrieve an adminstaffaccount by ID
        public static AdminStaffAccount retrieveAdminStaffAccountByID(string accountID)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                return
                    (from a in db.AdminStaffAccounts
                    where a.ASAccountID == accountID
                    select a).SingleOrDefault();
            }
        }

        //Retrieve an AdminStaffAccount by Username
        public static AdminStaffAccount retrieveAdminStaffAccountByUsername(string username){
            AdminStaffAccount account = null;
            using (hireddbEntities db = new hireddbEntities())
            {
                    account =
                    (from a in db.AdminStaffAccounts
                     where a.Username == username
                     select a).SingleOrDefault();
            }
            return account;
        }

        public static AdminStaffAccount retrieveAdminStaffAccountByEmail(string email)
        {
            using (hireddbEntities db = new hireddbEntities())
            {
                return (AdminStaffAccount)
                    (from a in db.AdminStaffAccounts
                     where a.Email == email
                     select a).SingleOrDefault();
            }
        }

        //Search an adminstaffaccount by ID
        public static IEnumerable<AdminStaffAccount> searchAdminStaffAccount(string searchQuery) {
            using (hireddbEntities db = new hireddbEntities())
            {
                IEnumerable<AdminStaffAccount> searchResults =
                    from a in db.AdminStaffAccounts
                    where SqlMethods.Like(a.Email.ToUpper(), searchQuery.ToUpper()) || SqlMethods.Like(a.Username.ToUpper(), searchQuery.ToUpper())
                    select a;
              
                return searchResults;
            }
        }

        //Delete an adminstaffaccount
        public static bool deleteAdminStaffAccount(AdminStaffAccount asa) {
            try
            {
                AdminStaffAccount accountToDelete = retrieveAdminStaffAccountByID(asa.ASAccountID);
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(accountToDelete);
                    db.DeleteObject(accountToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch {
                return false;
            }
        }
        public static bool deleteAdminStaffAccountById(string id)
        {
            try
            {
                AdminStaffAccount accountToDelete = retrieveAdminStaffAccountByID(id);
                using (hireddbEntities db = new hireddbEntities())
                {
                    db.Attach(accountToDelete);
                    db.DeleteObject(accountToDelete);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Update an adminstaffaccount
        public static bool updateAdminStaffAccount(AdminStaffAccount update) {
            try
            {
                using (hireddbEntities db = new hireddbEntities())
                {
                    AdminStaffAccount accountToUpdate = retrieveAdminStaffAccountByID(update.ASAccountID);
                    db.Attach(accountToUpdate);
                    accountToUpdate.Email = update.Email;
                    accountToUpdate.Username = update.Username;
                    accountToUpdate.Password = update.Password;
                    db.SaveChanges();
                }
                return true;
            }
            catch {
                return false;
            }
        }
    }
}